const Service = require('egg').Service;
const { Sequelize, Op } = require('sequelize');

class StuinsmsgService extends Service {
    // 添加用service
    async add(msg) {
        const { ctx } = this;
        const result = await ctx.model.Stuinsmsg.create(msg);
        return result;
    }
    // 分页查询用service
    async findList(keywords, pageIndex, pageSize) {
        const { ctx } = this;
        const result = await ctx.model.Stuinsmsg.findAndCountAll({
            where: {
                content: {
                    [Op.like]: '%' + keywords + '%'
                }
            },
            include:[
                {
                    model:ctx.model.Student,
                    as:'student'
                },
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                },
            ],
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
    // 根据id查找对应的值
    async findById(id) {
        const { ctx } = this;
        const result = await ctx.model.Stuinsmsg.findOne({
            where:{
                id:id
            },
            include:[
                {
                    model:ctx.model.Student,
                    as:'student'
                },
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                }
            ],
        });
        return result;
    }
    // 修改
    async modify(topic) {
        const { ctx } = this;
        const { 
            id,
            content
        } = topic;
        const result = await ctx.model.Stuinsmsg.update({
            content
        }, {
            where: { id }
        });
        return result;
    }
    // 删除
    async remove(id) {
        const { ctx } = this;
        const result = await ctx.model.Stuinsmsg.destroy({
            where: { id }
        });
        return result;
    }
    // 根据学生id和progress选择出最新的理由
    async findByStuidPro(stu_id,progress) {
        const { ctx } = this;
        const result = await ctx.model.Stuinsmsg.findAll({
            
            where:{
                stu_id,
                progress,
            },
            include:[
                // {
                //     model:ctx.model.Student,
                //     as:'student'
                // },
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                }
            ],
            order:[
                ['createdAt', 'DESC']
            ]
        });
        return result;
    }
    // 根据学生id选择出和这个学生有关系的消息记录
    async findbystuiddesc(keywords, pageIndex, pageSize,stu_id) {
        const { ctx } = this;
        const result = await ctx.model.Stuinsmsg.findAndCountAll({
            where:{
                stu_id,
                content: {
                    [Op.like]: '%' + keywords + '%'
                }
            },
            include:[
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                }
            ],
            order:[
                ['created_at', 'DESC']
            ],
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
            
        });
        return result;
    }
    // 老师通过学生的文档，用这个方法插入修改意见
    async passAdd(msg) {
        const { ctx } = this;
        const result = await ctx.model.Stuinsmsg.create(msg);
        const result2 = await ctx.service.student.modify({
            id:msg.stu_id,
            progress:msg.progress,
        });
        const final = {
            result,
            result2,
        }
        return final;
    }
}
module.exports = StuinsmsgService;
